"use strict";
var app = angular.module("myapp", []);

app.controller("HomeworkController", function($scope) {
	console.log(homeworks1);
	var data = eval("(" + homeworks1 + ")");
	// console.log(data);
	var uploaded_homeworks = [];
	var uploaded_passed_homeworks = [];
	var uncommitted_users = [];
	var task = {};
	if (data.error != 0) {
		alert(data.msg);
	} else {
		uncommitted_users = data.users;
		console.log(uncommitted_users);
		task = data.task;
		console.log("本次作业信息：" + task);

		var homeworks = data.homeworks;
		for ( var i = 0, n = homeworks.length; i < n; i++) {
			console.log(homeworks[i]);
			var homework = homeworks[i];
			if (homework.pass == 'true') {
				uploaded_passed_homeworks.splice(0, 0, homework);
			} else {
				uploaded_homeworks.splice(0, 0, homework);
			}
		}
	}
	$scope.task = task;
	$scope.uncommitted_users = uncommitted_users;
	$scope.uploaded_homeworks = uploaded_homeworks;
	$scope.uploaded_passed_homeworks = uploaded_passed_homeworks;
	$scope.mousein = function(row) {
		$scope.selectedRow = row;
	};
	$scope.remove = function(row, index) {

	};
	$scope.correct = function(row) {
		var homework = $scope.uploaded_homeworks[row];
		$('#correct_homework_content').html(homework.content);
		if (homework.attachID == -1) {
			$('#correct_homework_attach').html("没有附件");
		} else {
			$('#correct_homework_attach').html("<a href=\"download?fileID=\"" + homework.attachID + " data-toggle=\"tooltip\" data-placement=\"left\" title=\"下载\">" + "<span class=\"glyphicon glyphicon-link\">&nbsp;" + homework.attach + "</span></a>");
		}
		$('#correct_homework_note').html(homework.note);
		//$('#preview_homework_pass').html(homework.pass);
		$('#preview_homework_score').html(homework.score);
		$('#correct_homework_id').attr("value",homework.id);
		$('#row_num').attr("value",row);
		$('#modal_correct_homework').modal();
	};
	$scope.review = function(row) {
		var homework = $scope.uploaded_passed_homeworks[row];
		$('#preview_homework_content').html(homework.content);
		if (homework.attachID == -1) {
			$('#preview_homework_attach').html("没有附件");
		} else {
			$('#preview_homework_attach').html("<a href=\"download?fileID=\"" + homework.attachID + " data-toggle=\"tooltip\" data-placement=\"left\" title=\"下载\">" + "<span class=\"glyphicon glyphicon-link\">&nbsp;" + homework.attach + "</span></a>");
		}
		$('#preview_homework_note').html(homework.note);
		$('#preview_homework_pass').html(homework.pass);
		$('#preview_homework_score').html(homework.score);
		$('#preview_homework_evaluation').html(homework.evaluation);
		$('#preview_modal_homework').modal();
	};
	$scope.remind = function(row) {

	};
});
$(function() {
	// $('input[type=file]').bootstrapFileInput();
	
	var submit_btn = $('#submit_btn');
	var reset_btn = $('#reset_btn');
	
	var status = $('#status');
	
	$('#correct_homework_form').ajaxForm({
		beforeSend : function() {
			status.empty();
			submit_btn.attr("disabled", "disable");
			reset_btn.attr("disabled", "disable");
		},
		success : function() {
		},
		complete : function(xhr) {
			console.log(xhr.responseText);
			var res = eval("(" + eval(xhr.responseText) + ")");
			console.log(res);
			submit_btn.removeAttr("disabled");
			submit_btn.removeClass("disabled");
			reset_btn.removeAttr("disabled");
			reset_btn.removeClass("disabled");
			if (res.error != 0) {
				// alert(res.msg);
				$("#submit-btn").removeAttr("disabled");
				$("#submit-btn").removeClass("disabled");
				status.html(res.msg);
			} else {
				status.empty();
				status.html("提交成功,3秒后自动关闭。。。");
				setTimeout(function() {
					$('#reset_btn').click();
					$('#modal_correct_homework').modal('hide');
					var scope = angular.element($("#task_detail_div")).scope();
					var homework = scope.uploaded_homeworks[res.rowNum];
					scope.uploaded_homeworks.splice(res.rowNum, 1);
					scope.uploaded_passed_homeworks.splice(0, 0,homework);
					scope.$apply();
					status.empty();
				}, 3000);
			}

			// status.html("");

		}
	});

});
